/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src;

import java.util.ArrayList;
import java.util.List;
import org.primefaces.component.tabview.Tab;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.primefaces.event.NodeSelectEvent;

/**
 *
 * @author Yusup Jauhari Shandi <chan at In.Visi Forever!>
 */
public class Module implements java.io.Serializable{
    private String modulName;
    private TreeNode treeNode;
    private TreeNode rootNode;
    private List<Menu> menuList;
    private TreeNode selectedNode;  
     private int tabActiveIndex;
    
    public Module(String modulName) {
        this.modulName = modulName;
        menuList = new ArrayList<Menu>();
        rootNode = new DefaultTreeNode("root",null);
    }

    public Module() {
        menuList = new ArrayList<Menu>();
        rootNode = new DefaultTreeNode("root",null);
    }

    public int getTabActiveIndex() {
        return tabActiveIndex;
    }

    public void setTabActiveIndex(int tabActiveIndex) {
        this.tabActiveIndex = tabActiveIndex;
    }
    
    

    public void addMenuList(Menu menu,TreeNode parent){
        if (parent==null)
            parent = rootNode;
        menuList.add(menu);
        TreeNode t = new DefaultTreeNode(menu,parent);
    }
    public List<Menu> getMenuList() {
        return menuList;
    }

    public void setMenuList(List<Menu> menuList) {
        this.menuList = menuList;
    }

    
    public TreeNode getTreeNode() {
        return treeNode;
    }

    public TreeNode getRootNode() {
        return rootNode;
    }

    public void setRootNode(TreeNode rootNode) {
        this.rootNode = rootNode;
    }

    
    public void setTreeNode(TreeNode treeNode) {
        this.treeNode = treeNode;
    }

    
    public String getModulName() {
        return modulName;
    }

    public void setModulName(String modulName) {
        this.modulName = modulName;
    }
    
    
    public TreeNode getSelectedNode() {  
        return selectedNode;  
    }  
  
    public void setSelectedNode(TreeNode selectedNode) {  
        this.selectedNode = selectedNode;  
    }  
    
    public void onNodeSelect(NodeSelectEvent event) {  
     //   FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", event.getTreeNode().getData().toString());  
        //  increment();
      //  FacesContext.getCurrentInstance().addMessage(null, msg);  
         Menu x = (Menu)event.getTreeNode().getData();
         if (!this.isExistMenuOnTabs(x.getMenuId())){
             menuList.add(x);
             tabActiveIndex = menuList.size()-1;
         }
    }  
    
    public TreeNode getTreeNodeFromId(Integer id,TreeNode parent){
        TreeNode x =null;
        if (parent==null)
                parent = rootNode;
        for(int i=0;i<parent.getChildCount();i++){
          TreeNode z = parent.getChildren().get(i);  
          Menu menu = (Menu)z.getData();
          if (menu.getMenuId().equals(id)){
              x = z;
              break;
          }
        }
        return x;
    }
    
    private boolean isExistMenuOnTabs(Integer id){
          // Search for element in list
        boolean found = false;
       for (int i=0;i<menuList.size();i++)   {
           Menu m = menuList.get(i);
           if (m.getMenuId().equals(id)) {
               found = true;
               tabActiveIndex = i;
               break;
           }
       }
        return found;
    }
    
}
